package com.unitd.base.frame.plugin.db.util;

import java.io.BufferedReader;
import java.io.Reader;
import java.sql.Clob;

/**
 * oracle数据库转化工具
 *
 * @author Hudan
 * @version 1.0
 * @filename ClobUtil.java
 * @copyright www.unitd.com
 * @date 2020-06-28
 */
public class ClobUtil {

    /**
     * 将Oracle的Clob类型转换成String类型
     *
     * @param clob oracle的clob数据类型
     * @return 转换后的字符串
     */
    public static String clobToString(final Clob clob) {

        if (clob == null) {
            return null;
        }

        Reader reader = null;
        try {
            reader = clob.getCharacterStream();
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (reader == null) {
            return "";
        }

        String str = null;
        BufferedReader br = new BufferedReader(reader);
        // 读取第一行
        try {
            str = br.readLine();
        } catch (Exception e) {
            e.printStackTrace();
        }

        StringBuilder sb = new StringBuilder();

        // 如果没有到达流的末尾，则继续读取下一行
        while (str != null) {
            sb.append(str);
            try {
                str = br.readLine();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }
}